//
//  RCVoiceVC.swift
//  RunChuanThermometer
//
//  Created by 李东 on 2024/4/18.
//

import UIKit
import SnapKit

class RCVoiceVC: RCBaseVC {
    
    lazy var dataList1 = [RCVoiceModel(name: "爆竹"), RCVoiceModel(name: "电锯"),
                          RCVoiceModel(name: "鼓声"), RCVoiceModel(name: "火车"),
                          RCVoiceModel(name: "圣诞"), RCVoiceModel(name: "轮船"),
                          RCVoiceModel(name: "派对喇叭")]
    lazy var dataList2 = [RCVoiceModel(name: "猫咪"), RCVoiceModel(name: "狗狗"),
                          RCVoiceModel(name: "海豚"), RCVoiceModel(name: "鲸鱼"),
                          RCVoiceModel(name: "绵羊"), RCVoiceModel(name: "大象"),
                          RCVoiceModel(name: "老虎"), RCVoiceModel(name: "奶牛")]
    
    var collectionView: UICollectionView!
    var seletctView: RCVoiceSelectView!
    var isShowLeft = true
    lazy var player = AudioPlayerManager()
    var leftSelectIndex = 0
    var rightSelectIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "趣味音效"
        showBgImgView = true
        initUI()
        seletctView.selectCallBack = {[weak self] isLeft in
            guard let self = self else { return }
            self.isShowLeft = isLeft
            self.collectionView.reloadData()
        }
        player.playFinish = {[weak self] in
            guard let self = self else { return }
            if self.isShowLeft {
                let mm = self.dataList1[self.leftSelectIndex]
                mm.isPlaying = false
                self.dataList1[leftSelectIndex] = mm
            }else {
                let mm = self.dataList2[self.rightSelectIndex]
                mm.isPlaying = false
                self.dataList2[rightSelectIndex] = mm
            }
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        player.audioStop()
    }
    
    var dataList: [RCVoiceModel] {
        return isShowLeft ? dataList1 : dataList2
    }
    
    func initUI() {
        seletctView = RCVoiceSelectView()
        view.addSubview(seletctView)
        seletctView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(45)
            make.width.equalTo(150)
            make.top.equalToSuperview()
        }
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.itemSize = CGSize(width: (CGFloat.screenWidth-40)/2, height: 160)
        layout.sectionInset = UIEdgeInsets(top: 10, left: 15, bottom: 30, right: 15)
        layout.scrollDirection = .vertical
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: "RCVoiceCell", bundle: nil), forCellWithReuseIdentifier: RCVoiceCell.CellID)
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(seletctView.snp.bottom)
        }
    }

    func playVoice()  {
        var mm: RCVoiceModel!
        if isShowLeft {
            mm = dataList1[leftSelectIndex]
            if mm.isPlaying {
                mm.isPlaying = false
                dataList1[leftSelectIndex] = mm
                player.audioStop()
            }else{
                mm.isPlaying = true
                dataList1[leftSelectIndex] = mm
                player.initPlayers(filePath: mm.mp3Url)
            }
          
        }else{
            mm = dataList2[rightSelectIndex]
            if mm.isPlaying {
                mm.isPlaying = false
                dataList2[rightSelectIndex] = mm
                player.audioStop()
            }else{
                mm.isPlaying = true
                dataList2[rightSelectIndex] = mm
                player.initPlayers(filePath: mm.mp3Url)
            }
        }
     
    }
    
    func stopPlay() {
        var mm: RCVoiceModel!
        if isShowLeft {
            mm = dataList1[leftSelectIndex]
            if mm.isPlaying {
                player.audioStop()
            }
            mm.isPlaying = false
            dataList1[leftSelectIndex] = mm
        }else{
            mm = dataList2[rightSelectIndex]
            if mm.isPlaying {
                player.audioStop()
            }
            mm.isPlaying = false
            dataList2[rightSelectIndex] = mm
        }
    }

}

extension RCVoiceVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RCVoiceCell.CellID, for: indexPath) as! RCVoiceCell
        cell.model = dataList[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if isShowLeft {
            leftSelectIndex = indexPath.row
        }else{
            rightSelectIndex = indexPath.row
        }
        playVoice()
    }
}
